package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.common.response.sys.SysUserCommentResponse;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysUserComment;
import com.mars.module.admin.entity.SysUserLikeComment;
import com.mars.module.admin.mapper.SysUserCommentMapper;
import com.mars.module.admin.mapper.SysUserLikeCommentMapper;
import com.mars.module.admin.request.SysUserCommentRequest;
import com.mars.module.admin.service.ISysUserCommentService;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户评论业务层处理
 *
 * @author mars
 * @date 2024-03-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserCommentServiceImpl implements ISysUserCommentService {

    private final SysUserCommentMapper baseMapper;
    private final SysUserMapper userMapper;
    private final SysUserLikeCommentMapper commentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserComment add(SysUserCommentRequest request) {
        SysUserComment entity = SysUserComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserCommentRequest request) {
        SysUserComment entity = SysUserComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserComment getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserComment> pageList(SysUserCommentRequest request) {
        Page<SysUserComment> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserComment> query = this.buildWrapper(request);
        IPage<SysUserComment> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }

    @Override
    public List<SysUserComment> list(SysUserCommentRequest request) {
        LambdaQueryWrapper<SysUserComment> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public List<SysUserCommentResponse> tree(SysUserCommentRequest request) {
        List<SysUserComment> list = baseMapper.selectList(this.buildWrapper(request));
        List<SysUserCommentResponse> voList = new ArrayList<>();
        Long userId = ContextUserInfoThreadHolder.get().getId();
        for (SysUserComment sysMenu : list) {
            SysUserCommentResponse sysMenuVo = new SysUserCommentResponse();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                SysUser sysUser = userMapper.selectById(sysMenu.getUserId());
                sysMenuVo.setUserName(sysUser.getUserName());
                Long l = commentMapper.selectCount(new QueryWrapper<SysUserLikeComment>().eq("user_id", userId).eq("comment_id", sysMenuVo.getId()));
                if (l > 0) {
                    sysMenuVo.setIsLike("1");
                } else {
                    sysMenuVo.setIsLike("0");
                }
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    public List<SysUserCommentResponse> getChildren(List<SysUserComment> list, Long id) {
        List<SysUserCommentResponse> voList = new ArrayList<>();
        Long userId = ContextUserInfoThreadHolder.get().getId();
        for (SysUserComment sysMenu : list) {
            SysUserCommentResponse sysMenuVo = new SysUserCommentResponse();
            if (sysMenu.getParentId() != null && sysMenu.getParentId().equals(id)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                SysUser sysUser = userMapper.selectById(sysMenu.getUserId());
                sysMenuVo.setUserName(sysUser.getUserName());
                Long l = commentMapper.selectCount(new QueryWrapper<SysUserLikeComment>().eq("user_id", userId).eq("comment_id", sysMenuVo.getId()));
                if (l > 0) {
                    sysMenuVo.setIsLike("1");
                } else {
                    sysMenuVo.setIsLike("0");
                }
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    private LambdaQueryWrapper<SysUserComment> buildWrapper(SysUserCommentRequest param) {
        LambdaQueryWrapper<SysUserComment> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysUserComment::getContent, param.getContent());
        }
        if (param.getArticleId() != null) {
            query.eq(SysUserComment::getArticleId, param.getArticleId());
        }
        return query;
    }

}
